package network;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;


public class TCPSender extends Thread {
	
	private String filePath;
	private InetAddress destAddr;
	private int portEnvoi;
	private byte[] buf;
	private ChatNI nInt;
	
	public TCPSender (ChatNI nI, String pathFile, InetAddress addrD, int portTCP){
		this.filePath = pathFile;
		this.destAddr=addrD;
		this.portEnvoi = portTCP;
		this.buf = new byte [2048];	
		this.nInt=nI;
	}
	
	public void run(){
		sendFile();
		//nInt.dlComplete(destAddr);
	}
	
	private void sendFile () {
		int i;
		try {
			Socket sock = new Socket(destAddr,portEnvoi);
			FileInputStream f = new FileInputStream(new File(filePath));
			while ((i=f.read(buf))!=-1){
				sock.getOutputStream().write(buf,0,i);
			}
			
			f.close();
			sock.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} 
			
		
	}

}
